﻿Namespace Forms.Edit

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class frmLectureStudentDetails
        Inherits org.codegen.win.controls.frmBaseEdit

#Region "Designer"
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Friend WithEvents UcLectureStudent As ucLectureStudentDetails

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.UcLectureStudent = New IIC.ucLectureStudentDetails()
            Me.SuspendLayout()
            '
            'UcEditToolar
            '
            Me.UcEditToolar.ShowAdd = True
            Me.UcEditToolar.ShowDelete = True
            Me.UcEditToolar.ShowNavigationButtons = True
            Me.UcEditToolar.Size = New System.Drawing.Size(861, 30)
            '
            'UcLectureStudent
            '
            Me.UcLectureStudent.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange
            Me.UcLectureStudent.Dock = System.Windows.Forms.DockStyle.Fill
            Me.UcLectureStudent.isInitialized = False
            Me.UcLectureStudent.Location = New System.Drawing.Point(0, 30)
            Me.UcLectureStudent.ModelObject = Nothing
            Me.UcLectureStudent.Name = "UcLectureStudent"
            Me.UcLectureStudent.Size = New System.Drawing.Size(861, 378)
            Me.UcLectureStudent.studentId = 0
            Me.UcLectureStudent.TabIndex = 0
            '
            'frmLectureStudentDetails
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(861, 430)
            Me.Controls.Add(Me.UcLectureStudent)
            Me.KeyPreview = False
            Me.Name = "frmLectureStudentDetails"
            Me.ShowAdd = True
            Me.ShowDelete = True
            Me.ShowNavigationButtons = True
            Me.Text = "frmLectureStudent"
            Me.Controls.SetChildIndex(Me.UcEditToolar, 0)
            Me.Controls.SetChildIndex(Me.UcLectureStudent, 0)
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub

#End Region

#Region "Standard Code"

        Public Overrides Sub LoadData()

            Me.UcLectureStudent.loadData()
            Me.UcLectureStudent.UcLectureList.grdLecture.ClearSelectedRows()

            
        End Sub


        Public Overrides Function SaveData() As enumSaveDataResult

            If Me.ValidateChildren() Then

                Dim participants As List(Of LectureStudent) = New List(Of LectureStudent)
                Dim db As New LectureStudentDBMapper()
                Dim lectGrid As org.codegen.win.controls.Grid.CGSQLGrid = Me.UcLectureStudent.UcLectureList.grdLecture

                For Each row As DataGridViewRow In lectGrid.SelectedRows
                    Dim lectureId As Integer = CInt(row.Cells("LectureId").Value)
                    Dim lecture As Lecture = LectureDataUtils.findByKey(lectureId)
                    If (lecture Is Nothing) Then
                        Throw New ApplicationException("No lecture exists with id:" & lectureId)
                    End If

                    'check if student is already in the lecture
                    If lecture.Members.FirstOrDefault(Function(x)
                                                          Return x.StudentId.GetValueOrDefault = Me.UcLectureStudent.studentId
                                                      End Function) Is Nothing Then

                        Dim ls As LectureStudent = LectureStudentFactory.Create
                        ls.StudentId = Me.UcLectureStudent.studentId
                        ls.LectureId = lectureId
                        participants.Add(ls)

                    End If

                Next

                Me.lblEditStatus.Text = "Student " & Me.UcLectureStudent.StudentNameLbl.Text & " Added to Lecture(s)"
                Me.lblEditStatus.ForeColor = Color.Red

                Me.UcLectureStudent.studentId = 0

                db.saveList(participants)
                If Me.Owner IsNot Nothing AndAlso (TypeOf (Me.Owner) Is frmBaseGrid) Then
                    CType(Me.Owner, frmBaseGrid).grdData.requery()
                End If
                Return enumSaveDataResult.SAVE_SUCESS_AND_STAY 'we need the form to stay open so they can add multiple students
            Else
                Return enumSaveDataResult.SAVE_FAIL
            End If

        End Function

        ''' <summary>
        ''' Because this form is not closing after save, we keep it open 
        ''' and we do not show the "record not saved OK" that happens if we return 
        ''' false in the SaveData
        ''' </summary>
        ''' <remarks></remarks>
        Public Overrides Sub setAlertStatusRecordFailed()

            'Me.lblEditStatus.Text = txt
            'Me.lblEditStatus.ForeColor = Color.Red
        End Sub

        Public Overrides Function dataChanged() As Boolean

            Return Me.UcLectureStudent.hasChanges

        End Function

        ''' <summary>
        ''' On the load, we need to call "resetLastLoadedValues"
        ''' at the **end** of the method.  This call keeps track of the 
        ''' loaded data on the page, for the 'datachanged' functionality
        ''' </summary>
        Private Sub frm_Load(ByVal sender As Object, _
                                ByVal e As System.EventArgs) Handles Me.Load

            '**** any code should go before "resetLastLoadedValues"

            'selectLecture
            If Application.OpenForms("frmMain") IsNot Nothing Then

                Dim nd As TreeNode = CType(Application.OpenForms("frmMain"), frmMain).ucSystemTree.tvMain.SelectedNode
                If TypeOf (nd.Tag) Is Lecture Then
                    Dim lect As Lecture = CType(nd.Tag, Lecture)
                    Dim lessonCode As String = lect.Lesson.LessonCode
                    For Each dr As DataGridViewRow In Me.UcLectureStudent.UcLectureList.grdLecture.Rows
                        If CStr(dr.Cells("LessonCode").Value) = lessonCode Then
                            dr.Selected = True
                        Else
                            dr.Selected = False
                        End If
                    Next
                End If

            End If

            '***** IMPORTANT: keep this call at the end of the procedure
            Call Me.UcLectureStudent.resetLastLoadedValues()
            
        End Sub

#End Region

    End Class

End Namespace

